﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.Utilities;
using FrameworkManapro.WebUI;

namespace GMAC.ReportingSystem.Business.Components
{
    public class DealerComponent : IBusinessComponent<Entities.Dealer>
    {
        private DataAccess.DealerDataAccess dac;

        public DealerComponent()
        {
            dac = new DataAccess.DealerDataAccess();
        }

        #region IBusinessComponent<Dealer> Members

        public int Create(Entities.Dealer entityToCreate)
        {
            if (Validate(entityToCreate))
            {
                var result = dac.Insert(entityToCreate);
                return result;
            }
            return 0;
        }

        public int Delete(Entities.Dealer entityToDelete)
        {
            if (entityToDelete.DealerID <= 0)
                throw new ArgumentOutOfRangeException("DealerID", "Value must be greater than zero.");

            return dac.Delete(entityToDelete);
        }

        public int Edit(Entities.Dealer entityToEdit)
        {
            if (entityToEdit.DealerID <= 0)
                throw new ArgumentOutOfRangeException("DealerID", "Value must be greater than zero.");

            return Validate(entityToEdit) ? dac.Update(entityToEdit) : 0;
        }

        public Entities.Dealer GetDetails(object entityId)
        {
            return dac.Get(entityId);
        }

        protected bool Validate(Entities.Dealer Dealer)
        {
            if (Dealer == null) throw new ArgumentNullException("Dealer", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Dealer.DealerFI.ToString())) throw new ArgumentNullException("DealerFI", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Dealer.DealerType.ToString())) throw new ArgumentNullException("DealerType", "The argument cannot be null.");

            return true;
        }

        #endregion


        public Entities.DealerCollection GetList()
        {
            return dac.GetList();
        }


        public Entities.DealerCollection Search(string Dealername, string DealerNumber, PagingDetails paging)
        {
            if (Dealername == null) Dealername = string.Empty;
            if (DealerNumber == null) DealerNumber = string.Empty;
            return dac.Search(Dealername, DealerNumber, paging);
        }



    }
}
